#include "TestGame.h"
#include "Renderer.h"
#include "pg1_input.h"

using namespace ElJuego;
//---------------------------------------

//---------------------------------------
 TestGame::TestGame(){
	 //quad = new Quad();
	 //quadTwo = new pg1::Quad();
 }
bool TestGame::init (pg1::Renderer& renderer)
{
	quad.setPos(0.0f, 0.0f);
	quad.setRotation(0.0f);
	quad.setScale(20.0f,50.0f);

	quadTwo.setPos(10.0f, 100.0f);
	quadTwo.setRotation(0.0f);
	quadTwo.setScale(100.0f,50.0f);
	
	//Sprite1
	sprite.setScale(100.0f,100.0f);
	sprite.setPos(-100.0f,0.0f);
	/*sprite.setTextureCoords(512.0f/1024.0f,5.0f/512.0f,
							(512.0f+148.0f)/1024.0f,5.0f/512.0f,
							512.0f/1024.0f,(5.0f+164.0f)/512.0f,
							(512.0f+148.0f)/1024.0f,(5.0f+164.0f)/512.0f);*/

	//Sprite2
	sprite2.setScale(100.0f,100.0f);
	sprite2.setPos(0,0.0f);
	//Texturas
	pg1::Texture kTank = renderer.loadTexture("BC_TANKSHEET.jpg",PG1_COLOR_RGB(0,0,0));
	sprite.setTexture(kTank);
	
	pg1::Texture kMap = renderer.loadTexture("BC_MAP.jpg",PG1_COLOR_RGB(0,0,0));
	sprite2.setTexture(kMap);

	m_kIdle.setLength(1500);
	//addframe y set animation
	m_kIdle.addFrame(400.0f,256.0f,	1.0f,	2.0f,	10.0f,	14.0f);
	m_kIdle.addFrame(400.0f,256.0f,	1.0f,	2.0f,	10.0f,	14.0f);
	m_kIdle.addFrame(400.0f,256.0f,	13.5f,	2.0f,	10.0f,	14.0f);
	sprite.setAnimation(&m_kIdle);

	m_kIdle2.setLength(1500);
	m_kIdle2.addFrame(400.0f,256.0f,	1.0f,	2.0f,	10.0f,	14.0f);
	m_kIdle2.addFrame(400.0f,256.0f,	1.0f,	2.0f,	10.0f,	14.0f);
	m_kIdle2.addFrame(400.0f,256.0f,	13.5f,	2.0f,	10.0f,	14.0f);
	sprite2.setAnimation(&m_kIdle2);

	m_kObjects.push_back(quad);
	m_kObjects.push_back(quadTwo);
	return true;
}
//---------------------------------------
void TestGame::frame (pg1::Renderer& renderer,pg1::DirectInput& rkInput,pg1::Timer& rkTimer)
{
	static float fSpeed= 0.5f;

	float newPosX = sprite.posX();
	float newPosY = sprite.posY();

	if(rkInput.keyDown(pg1::Input::KEY_UP)) {
		newPosY += fSpeed;
	}
	if(rkInput.keyDown(pg1::Input::KEY_DOWN)) {
		newPosY -= fSpeed;
	}
	if(rkInput.keyDown(pg1::Input::KEY_RIGHT)) {
		newPosX += fSpeed;
	}
	if(rkInput.keyDown(pg1::Input::KEY_LEFT)) {
		newPosX -= fSpeed;
	}

	sprite.setPos(newPosX, newPosY);
	
	//Colisiones
	for (std::list<pg1::Entity2D>::iterator it = m_kObjects.begin(); it != m_kObjects.end(); it++){
		if(sprite.checkCollision(*it) == pg1::Entity2D::CollisionHorizontal)
		{
			sprite.returnToPos(sprite.prevPosX(), sprite.posY());
		}
		if(sprite.checkCollision(*it) == pg1::Entity2D::CollisionVertical)
		{
			sprite.returnToPos(sprite.posX(), sprite.prevPosY());
		}
	}
	//Fin Colisiones
	static float quadOneRotationSpeed=1.0f;
	//quadOneRotationSpeed+=0.1f;
	//quad.setRotation(quadOneRotationSpeed);
	//render
	
	sprite.update(rkTimer);
	sprite2.update(rkTimer);
	
	sprite.draw(renderer);
	sprite2.draw(renderer);

	sprite.drawAABB(renderer);
	quad.draw(renderer);
	quadTwo.draw(renderer);
	
}
//---------------------------------------
void TestGame::deinit(){}